<?
// myHandbook: High-level display functions
// Gives look-and-feel to the Web site
// Written by John Shepherd, May 2007
// Assumes that "lib/defs.php" already included

libs("forms","session");
//libs("session");

// Functions related to pages

# logo:
# - returns HTML version of the logo (large or small)

function logo($title="",$link="",$size="large")
{
	//$top = URL."/index.html";
	$style = "font-size:".(($size == "large") ? "20pt" : "16pt");
	//return "<span style=\"".$style."\">".SYSNAME.": </span><a href=\"".$link."\" style='$style'>".$title."</a>";
   return "<span style=\"".$style."\">".SYSNAME.": ".$title."</span>";
}

# pageHeader:
# - build a standard MessageBoard page header
# - allows incorporation of style info and JavaScript

function pageHeader($title="",$style="",$styleMod="",$jsdefs="",$jsinit="")
{
	$html  = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
	$html .= "\n<html>\n<head>\n";
	if ($title != "") $html .= "<title>".SYSNAME.": $title</title>\n";
	$html .= "<meta http-equiv=\"Content-Type\" ";
	$html .= "content=\"text/html; charset=ISO-8859-1\">\n";
	$html .= "<meta http-equiv=\"Content-Script-Type\" ";
	$html .= "content=\"text/javascript\">\n";
	$html .= "<meta http-equiv=\"Pragma\" content='no-cache'>\n";
	$html .= "<meta http-equiv=\"expires\" content='0'>\n";
	if ($style != "") {
		$html .= "<link rel='stylesheet' type='text/css' ";
		$html .= "href='".URL."/lib/$style.css'>\n";
	}
	if (def($styleMod))
		$html .= "<style type='text/css'>\n$styleMod\n</style>\n";
	if (def($jsdefs))
		$html .= "$jsdefs\n";
	if (!def($jsinit))
		$html .= "</head>\n<body>\n";
	else {
		$html .= "<script type='text/javascript'>\n".
			 "function initOnLoad() {\n$jsinit\n}\n</script>\n".
			 "</head>\n<body onload='initOnLoad();'>\n";
      
	}
	return $html;
}

# pageHeader:
# - build a standard MessageBoard page header
# - allows incorporation of style info and JavaScript

function pageFooter()
{
	$html  = "<div class=\"footer\">\n".
               "<div class=\"contentStyling\">".
                  startForm().
                  "<b>Ask a question:</b>".
                  "<input name=\"classid\" type=\"hidden\" value=\"".userClassID()."\">".
                  "<input name=\"question\" type=\"text\" />".
                  inputReset("Reset").
                  inputSubmit("Submit Question").
                  endForm().
               "</div>\n".
            "</div>\n";
	return $html;
}

# startPage:
# - returns standard stuff to start HTML output
# - to ensure that all pages have the same look-and-feel

function startPage($titles="",$style="style",$styleMod="",
				$jsdefs="",$jsinit="",$keys="")
{
	$dontShowLogin = ($titles == "Login" || $titles == "Error");
   
   if ($titles == "") {
		$titles = array();
	} elseif (!is_array($titles)) {
		$titles = array($titles=>"");
   }
   
   foreach ($titles as $title => $link) {
      //$crumbs = logo($title, $link);
      $crumbs = logo($title);
      $htitle = $title;
   }
   
   /*$crumbs = logo();
   //$crumbs = "";
   $htitles = array();
   
   foreach ($titles as $title => $link) {
      if ($link != "") {
         $t = hyperlink($link,$title)."\n";
      } else {
         $t = "<b>$title</b>\n";
      }
      $crumbs .= " : $t";
      $htitles[] = $title;
   }
   $htitle = join(" : ",$htitles);*/
   
	if ($dontShowLogin) {
		$login = "";
		$sidebar = "";
		$div = "full";
	} else {
		$param = oneParam("returnto",thisURL());
      
		if (isLoggedIn()) {
			$name = userCourseName();
         //$type = userType();
			$login = "Logged into <b>$name</b>";
			$url = mkURL(URL."/session/logout.php",$param);
			$action = action($url,"Logout");
		} else {
			$login = "You are not logged into a course<br />";
			$url = mkURL(URL."/session/login.php",$param);
			$action = action($url,"Login");
		}
      
		$login .= " $action\n";
		//$sidebar = sidebar($keys);
      $sidebar = "";
		$div = "content";
	}
	$start = pageHeader($htitle,$style,$styleMod,$jsdefs,$jsinit);
	$heading =  "<div class=\"header\">\n".
                  "<div class=\"lHeader\">".
                     "<div class=\"contentStyling\">".
                        $crumbs."<br />".
                        date("G:i")." - ".
                        date("D, d M").
                        //date("G:i, D, d M")."<br />".
                        //"Date: ".date("D, d M")."<br />".
                        //"Time: ".date("G:i").
                     "</div>".
                  "</div>\n".
                  "<div class=\"rHeader\">".
                     "<div class=\"contentStyling\">".
                        $login.
                     "</div>".
                  "</div>\n".
               "</div>\n";
   $mainStart = "<div class=\"main\">\n";
   $contentStart =   "<div class=\"".$div."\">\n".
                        "<div class=\"contentStyling\">";
	return $start.$heading.$mainStart.$sidebar.$contentStart;
}


# endPage:
# - returns HTML to finish off a page

function endPage($isLogin=false)
{
	$start = getTimer();
	$end = microtime(true);
	$elapsed = sprintf("%0.1f", ($end - $start));
	$html =  "</div>\n</div>\n</div>\n";
   
   if (!$isLogin) {
      $html .= pageFooter();
   }
   
   $html .=  "</body>\n".
            jsTimer($elapsed)."\n".
            jsLoadQuestion("selectQuestion")."\n".
            "</html>\n";
            
   return $html;
}


# sidebar:
# - build the sidebar in the left-hand-DIV

function sidebar($keys="")
{
	$sidebar = array("Week", "Lesson");
	$base = URL;
   
	$html =  "<div class=\"sidebar\">\n".
               "<div class=\"contentStyling\"><b>Select a lesson to load current question</b>\n".
                  startForm().
	/*foreach ($sidebar as $dir) {
		$label = ucFirst($dir);
		$html .= "<p>$label</p>\n";
	}*/
                  lessonForm().
                  p(inputSubmit("Submit")).
                  endForm().
                  "<p style='font-size:9pt'>".
                     "Load time: <span id='timer'>0.0</span>s".
                  "</p>".
               "</div>\n".
            "</div>";
	return $html;
}

# Timer:

$_starting = 0.0;
function setTimer() { global $_starting; $_starting = microtime(true); }
function getTimer() { global $_starting; return $_starting; }
function jsTimer($t)
{
return <<<_JS_
<script type='text/javascript'>
document.getElementById("timer").innerHTML= '$t';
</script>
_JS_;
}
function jsLoadQuestion($formName)
{
return <<<_JS_
<script type="text/javascript">
function loadQuestion(element) {

document.$formName.submit();
}
</script>
_JS_;
}
?>
